package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/27 13:04
 */
public class 排序链表148 {
    public static void main(String[] args) {
        new 排序链表148().sortList(ListNode.getInstance());

    }

    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        //小堆
        PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
        while (head != null) {
            priorityQueue.add(head);
            head = head.next;
        }
        ListNode pre = new ListNode(-1);
        ListNode cur = pre;
        while (priorityQueue.size() > 0) {
            ListNode node = priorityQueue.poll();
            cur.next = node;
            cur = cur.next;
        }
        cur.next = null;
        return pre.next;

    }
}
